package com.mybatis.lo.sqlsource;

import com.mybatis.lo.config.DynamicContext;
import com.mybatis.lo.handler.ParameterMappingTokenHandler;
import com.mybatis.lo.mapping.BoundSql;
import com.mybatis.lo.parser.GenericTokenParser;
import com.mybatis.lo.sqlnode.SqlNode;

/**
 * 通用的SqlSource实现
 * @author zhangbh
 * 2023-06-21 07:40
 **/
public abstract class BaseSqlSource implements SqlSource {

    protected BoundSql getBoundSql(Object param, SqlNode mixedSqlNode) {
        DynamicContext context = new DynamicContext(param);
        ParameterMappingTokenHandler tokenHandler = new ParameterMappingTokenHandler();
        GenericTokenParser parser = new GenericTokenParser("#{", "}", tokenHandler);
        mixedSqlNode.apply(context);
        // 处理sql
        String parse = parser.parse(context.getSql());
        return new BoundSql(parse, tokenHandler.getParameterMappings());
    }
}
